package weixinkeji.dev.dc.base.scheme.combo.h;

import java.util.List;
import weixinkeji.dev.dc.base.scheme.combo.bean.Combo;
import sys.wx.c.ATR;
import sys.wx.c.components.cv.CV;
import sys.wx.ann.hm.H;
import sys.wx.ann.hm.M;
import sys.wx.m.db.SQL.mysql.SQLD;
import sys.wx.m.db.SQL.mysql.SQLI;
import sys.wx.m.db.SQL.mysql.SQLU;
import sys.wx.m.db.SQL.mysql.common.where.Where;
import sys.wx.m.db.dao.mysql.Dao;
import sys.wx.m.db.dao.mysql.Dao2;
import weixinkeji.dev.dc.base.scheme.combo.bean.ComboB;
import weixinkeji.dev.dc.base.scheme.combo.bean.fl.ComboFL;
import weixinkeji.dev.dc.base.scheme.combo.check.CheckCombo;

/**
 *
 * @author IK
 */
@H("weixinkeji/dev/dc/base/scheme/combo/combo/adu")
public class ComboADU {
//weixinkeji/dev/dc/base/scheme/combo/combo/adu/add.imy
//weixinkeji/dev/dc/base/scheme/combo/combo/adu/dell.imy
//weixinkeji/dev/dc/base/scheme/combo/combo/adu/update.imy

    ATR a;                  //ATR是必须要的。它封装了web请求的相关信息

    public ComboADU(ATR obj) {     //这个有参构造函数，也是必须的。它进行附值
        this.a = obj;
    }

    @M("/add")
    public String a1() {
        CheckCombo cc = new weixinkeji.dev.dc.base.scheme.combo.check.CheckCombo();
        Combo obj = CV.getObject_Add(Combo.class, a.request, cc.doCombo(false));
        List<ComboB> flist = CV.getObject_Json(ComboB.class, "list", a.request, cc.doComboB(false));

        if (null == obj || null == flist) {
            a.out_OK_NO(false, "您提交的数据存在格式问题。请注意数据字符的输入");
            return null;
        }
        Double listSum = 0.0;
        for (ComboB cb : flist) {//统计清单价格
            listSum = listSum + cb.getFood_priceSum() * 10000;
//            System.out.println("listSum+cb.getFood_priceSum()=" + listSum + "//" + cb.getFood_priceSum());
        }
        obj.setCombo_listPriceSum(listSum / 10000);
        a.out_OK_NO(Dao2.add_OM_Auto(obj, flist, null));
        return null;
    }

    @M("/dell")
    public String d() {
        String ids = CV.getParameter(a.request, "id");
        if (ids.length() < 26) {
            return null;
        }
        a.out_OK_NO(Dao2.dellVast_OO_OM(CV.toSQLStringValue(ids), Combo.class, ComboB.class));
        return null;
    }

    @M("/update")
    public String u() {
        return null != a.request.getParameter("id") ? select1(a.request.getParameter("id")) : update();
    }

    private String select1(String x) {
        Combo cb = Dao.selectOneByID(Combo.class, x);
        a.request.setAttribute("ComboFL", Dao.selectOneByID(ComboFL.class, cb.getFlid()));
        a.request.setAttribute("COMBO", cb);
        return "/dc_manager/base/scheme/combo/combo_U.jsp";
    }

    private String update() {
        CheckCombo cc = new weixinkeji.dev.dc.base.scheme.combo.check.CheckCombo();
        Combo obj = CV.getObject(Combo.class, a.request, cc.doCombo(true));
        List<ComboB> flist = CV.getObject_Json(ComboB.class, "list", a.request, cc.doComboB(true));

        if (null == obj || null == flist) {
            a.out_OK_NO(false, "您提交的数据存在格式问题。请注意数据字符的输入");
            return null;
        }
        Double listSum = 0.0;
        for (ComboB cb : flist) {
            listSum = listSum + cb.getFood_priceSum() * 10000;
        }
        obj.setCombo_listPriceSum(listSum / 10000);

        a.out_OK_NO(
                Dao2.sql(
                        SQLD.dell_My(ComboB.class, Where.putThenGet(Where.TJ_IGNORE_NOTHING, null, "",  "combo_id", "=", obj.getCombo_id()))//清空套餐旧清单
                        , SQLU.update_reject(obj, "combo_code")//修改套餐表头
                        , SQLI.insert(flist)//加入新的清单
                )
        );
        return null;
    }
}
